<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-10
 * Time: 16:25
 */

namespace app\manage\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

// 引入ZBuilder
use app\common\model\RoomType as RoomTypeModel;
use think\Db;

class RoomType  extends Admin
{

    public $roomType = [];
    public $roomStatus = [];
    protected function initialize()
    {
        $this->roomType = Db::name('qt_room_type')->where('store_id','1')->column('name','id');
        $this->roomStatus = Db::name('qt_room_status')->where('store_id','1')->column('name','id');
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    //房间类型列表
    public function index()
    {
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $btn_access = [
            'title' => '新增房间类型',
            'icon' => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href' => url('add'),
        ];
        $data_list = Db::name('qt_room_type')->where($map)
            ->where(['store_id'=>'1'])
            ->order($order)
            ->paginate();
        // 分页数据
        $page = $data_list->render();

        $btn_access1 = [
            'title' => '编辑房间类型',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href' => url('edit', ['id' => '__id__']),
        ];
        $btn_access2 = [
            'title' => '删除房间类型',
            'icon' => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default',
            'href' => url('delete', ['id' => '__id__']),
        ];
        //dump($this->roomType);die;
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumn('id', '编号', 'text')
            ->addColumn('name', '房间类型', 'text')
            ->addColumn('sort', '排序', 'text')
            ->addColumn('status', '状态', 'status','',['1'=>'显示:success','2'=>'隐藏:danger'])
            ->addColumn('right_button', '操作', 'btn')
            ->addTopButton('add', $btn_access)// 添加顶部按钮
            ->addRightButton('edit', $btn_access1)// 添加右侧按钮
            ->addRightButton('delete', $btn_access2)// 添加右侧按钮
            ->setPrimaryKey('id')// 设置主键名为username
            ->setPageTitle('房间类型列表')
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch();
    }

    //添加房间
    public function add()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            if (empty($data['name'])) {
                $this->error('请填写房间类型');
            }
            $data['created_time'] = time();
            $data['store_id'] = '1';
            if (RoomTypeModel::create($data)) {
                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        } else {

            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7', 'name', '房间名称'],
                        ['text:7', 'sort', '排序', '<code>排序1-100，越小越靠前</code>', '50'],
                        ['radio', 'status', '状态', '', [1 => '显示', 2 => '隐藏'], '1'],
                    ]
                )
                ->setPageTitle('添加房间状态')
                ->fetch();
        }
    }

    //编辑房间
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            if (empty($data['name'])) {
                $this->error('请填写房间状态');
            }
            if (RoomTypeModel::update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = RoomTypeModel::get($id);
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden', 'id', 'id'],
                        ['text:7', 'name', '房间名称'],
                        ['text:7', 'sort', '排序', '<code>排序1-100，越小越靠前</code>', '50'],
                        ['radio', 'status', '状态', '', [1 => '显示', 2 => '隐藏'], '1'],
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('编辑房间')
                ->fetch();
        }
    }

    //删除房间
    public function delete($id = null)
    {
        if ($id === null) $this->error('缺少参数');
        $re = RoomTypeModel::destroy($id);//
        if ($re) {
            $this->success('删除成功', 'index');
        } else {
            $this->error('系统繁忙请稍后再试', 'index');
        }

    }
}